package com.ymatou.iapi.sellercs.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.CloseInactionSessionBean;
import com.ymatou.iapi.sellercs.parameter.CsAutoOffLineBean;
import com.ymatou.iapi.sellercs.service.CloseInactionSessionCall;
import com.ymatou.iapi.sellercs.service.CsAutoOffLineCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtOnlinecs;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*下线不活跃的客服
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CsAutoOffLine{
	private static CsAutoOffLineBean csautoofflineBean;
	private static CsAutoOffLineCall csautoofflineCall;
	private static CloseInactionSessionBean closeinactionsessionBean;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	private static YmtSellerCSIMWapper YmtSellerCSIMWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("下线不活跃的客服");
	}

	@Before
	public void caseUp() {
		csautoofflineBean=new CsAutoOffLineBean();
		csautoofflineCall=new CsAutoOffLineCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(csautoofflineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CsAutoOffLine_001() {
		Logger.start(true,"下线PC不活跃的客服成功");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// Pc上线
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(sellerId, sellerId, SocketIoToken);
			
			YmtSellerCSIMWapper.updateYmtOnlinecsLastActiveTime(sellerId, YMTDateUtil.getDateBeforeOrNextSecond(-60*10));
			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, sellerId);
			Logger.debug("update:"+FormatUtil.beanToGSONString(infos));
			
			csautoofflineBean.setAppId("autotest");
			csautoofflineCall.setData(csautoofflineBean);
			csautoofflineCall.callService();			
			Logger.verifyEquals("ok", csautoofflineCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, sellerId);
			Logger.verifyEquals(0, infos.size(), "验证记录删除");
			Logger.debug("dojob:"+FormatUtil.beanToGSONString(infos));
//			Logger.debug(FormatUtil.beanToGSONString(infos));
//			YmtOnlinecs info = infos.get(0);
//			Logger.verifyEquals(sellerId, info.getCSId(), "验证csid");
//			Logger.verifyEquals(sellerId, info.getMasterCSId(), "验证mcsid");
//			
//			Logger.verifyEquals(0, info.getLoginStatus(), "验证loginStatus为0不在线");
//			Logger.verifyEquals(1 ,info.getStatus(), "验证status为1离线");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsAutoOffLine_002() {
		Logger.start(true,"最后消息时间未超过1分钟不关闭不活跃的会话");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// Pc上线
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(sellerId, sellerId, SocketIoToken);
			
//			YmtSellerCSIMWapper.updateYmtOnlinecsLastActiveTime(sellerId, YMTDateUtil.getDateBeforeOrNextSecond(-60*10));
			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, sellerId);
			Logger.debug("update:"+FormatUtil.beanToGSONString(infos));
			
			csautoofflineBean.setAppId("autotest");
			csautoofflineCall.setData(csautoofflineBean);
			csautoofflineCall.callService();			
			Logger.verifyEquals("ok", csautoofflineCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, sellerId);
			Logger.verifyEquals(1, infos.size(), "验证记录不删除");
			Logger.debug("dojob:"+FormatUtil.beanToGSONString(infos));
			Logger.debug(FormatUtil.beanToGSONString(infos));
			YmtOnlinecs info = infos.get(0);
			Logger.verifyEquals(sellerId, info.getCSId(), "验证csid");
			Logger.verifyEquals(sellerId, info.getMasterCSId(), "验证mcsid");
			
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为1pc");
			Logger.verifyEquals(0 ,info.getStatus(), "验证status为0");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsAutoOffLine_003() {
		Logger.start(true,"APP和PC同时在线下线PC不活跃的客服成功");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("删除旧数据");
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			// Pc上线
			String SocketIoToken=TestDataManager.getRandomUUID().toString();
			SellerCSIMCallService.PcLogin(sellerId, sellerId, SocketIoToken);
			SellerCSIMCallService.AppLogin(sellerId);
			YmtSellerCSIMWapper.updateYmtOnlinecsLastActiveTime(sellerId, YMTDateUtil.getDateBeforeOrNextSecond(-60*6));
			
			csautoofflineBean.setAppId("autotest");
			csautoofflineCall.setData(csautoofflineBean);
			csautoofflineCall.callService();			
			Logger.verifyEquals("ok", csautoofflineCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, sellerId);
			Logger.verifyEquals(1, infos.size(), "验证记录未删除");
			Logger.debug(FormatUtil.beanToGSONString(infos));
			YmtOnlinecs info = infos.get(0);
			Logger.verifyEquals(sellerId, info.getCSId(), "验证csid");
			Logger.verifyEquals(sellerId, info.getMasterCSId(), "验证mcsid");
			
			Logger.verifyEquals(2, info.getLoginStatus(), "验证loginStatus为2App在线");
			Logger.verifyEquals(0 ,info.getStatus(), "验证status为0");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}